<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #663399;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            width: 350px;
        }
        h2 {
            text-align: center;
            color: #663399;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .error {
            color: red;
            font-size: 12px;
            margin-top: 5px;
            height: 15px;
        }
        button {
            background-color: #663399;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            font-size: 16px;
        }
        button:hover {
            background-color: #5a2d8a;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username">
                <div id="usernameError" class="error"></div>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password">
                <div id="passwordError" class="error"></div>
            </div>
            <button type="submit">登录</button>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            // 阻止表单默认提交行为
            event.preventDefault();

            // 获取表单元素
            const usernameInput = document.getElementById('username');
            const passwordInput = document.getElementById('password');
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');

            // 清除之前的错误信息
            usernameError.textContent = '';
            passwordError.textContent = '';

            // 验证用户名
            if (!usernameInput.value.trim()) {
                usernameError.textContent = '用户名不能为空';
                usernameInput.focus();
                return;
            }

            if (usernameInput.value.length < 8 || usernameInput.value.length > 20) {
                usernameError.textContent = '用户名长度必须为8-20个字符';
                usernameInput.focus();
                return;
            }

            // 验证密码
            if (!passwordInput.value.trim()) {
                passwordError.textContent = '密码不能为空';
                passwordInput.focus();
                return;
            }

            if (passwordInput.value.length < 8 || passwordInput.value.length > 20) {
                passwordError.textContent = '密码长度必须为8-20个字符';
                passwordInput.focus();
                return;
            }

            // 所有验证通过，可以提交表单
            alert('登录信息验证通过！');
            // 这里可以添加实际的表单提交代码
            // this.submit();
        });
    </script>
</body>
</html>